﻿using System;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace LetsDoIt
{
	public partial class ucbuttonEx : UserControl, IDisposable
	{
		private string caption = string.Empty;
		private Color chroma;
		private Image ourimage;
		private Image bufferimage = null;
		private ImageAttributes imageattr = new ImageAttributes();
		private bool isPressed;
		private Color backColorPressed;
		private Color foreColorPressed;
		private Color backColorDisabled;
		private Color foreColorDisabled;

		public ucbuttonEx()
		{
			InitializeComponent();
		}

		public new bool Enabled
		{
			get { return base.Enabled; }
			set {
				bool toInvalidate = (base.Enabled != value);
				base.Enabled = value;
				if (toInvalidate)
				{
					bufferimage = null;
					this.Invalidate();
				}
			}
		}

		public new Font Font
		{
			get
			{
				return base.Font;
			}
			set
			{
				base.Font = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		public string Caption
		{
			get
			{
				return caption;
			}
			set
			{
				caption = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		public Image OurImage
		{
			get
			{
				return ourimage;
			}
			set
			{
				ourimage = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		public Color Chroma
		{
			get
			{
				return chroma;
			}
			set
			{
				chroma = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		public Color BackColorPressed
		{
			get
			{
				return backColorPressed;
			}
			set
			{
				backColorPressed = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		public Color ForeColorDisabled
		{
			get
			{
				return foreColorDisabled;
			}
			set
			{
				foreColorDisabled = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		public Color BackColorDisabled
		{
			get
			{
				return backColorDisabled;
			}
			set
			{
				backColorDisabled = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		public Color ForeColorPressed
		{
			get
			{
				return foreColorPressed;
			}
			set
			{
				foreColorPressed = value;
				bufferimage = null;
				this.Invalidate();
			}
		}

		private int radius = 4;
		private int Radius
		{
			get { return radius; }
			set { 
				radius = value;
				bufferimage = null;
				this.Invalidate();
			}
		}


		private void ucbutton_Paint(object sender, PaintEventArgs e)
		{
			//if (ourimage == null)
			//    return;

			if (bufferimage == null)
				draw_buffered_image();
			e.Graphics.DrawImage(bufferimage, this.ClientRectangle, 0,
				0, this.Width, this.Height, GraphicsUnit.Pixel,
				imageattr);
		}

		private void draw_buffered_image()
		{
			// set the inital size of our imagebuffer
			//int canvasheight = ourimage.Height;
			//int canvaswidth = ourimage.Width;

			SizeF fontsize = new SizeF();
			if (!string.IsNullOrEmpty(caption))
			{
				Bitmap tempbmp = new Bitmap(1, 1);
				Graphics tempgrphs = Graphics.FromImage(tempbmp);
				fontsize = tempgrphs.MeasureString(caption, this.Font);
				//canvasheight += Convert.ToInt32(fontsize.Height) + 2;
				tempgrphs.Dispose();
				tempbmp.Dispose();
			}

			imageattr.SetColorKey(chroma, chroma);

			// build a new image buffer
			bufferimage = new Bitmap(this.Width, this.Height);
			Graphics graphs = Graphics.FromImage(bufferimage);

			////draw icon
			//graphs.DrawImage(ourimage, destrect, 0, 0, ourimage.Width, ourimage.Height, GraphicsUnit.Pixel, imageattr);
			////draw text
			//if (!string.IsNullOrEmpty(caption))
			//{
			//    Brush ourbrush = new SolidBrush(this.ForeColor);
			//    int startpos = 0;
			//    // we're smaller than width of image, so lets try and centre
			//    if (fontsize.Width < canvaswidth)
			//    {
			//        startpos = Convert.ToInt32((canvaswidth - fontsize.Width) / 2);
			//    }
			//    graphs.DrawString(caption, this.Font, ourbrush, startpos, (float)ourimage.Height + 1);
			//}


			//graphs.
			Brush buttonBrush = new SolidBrush(Enabled ?( isPressed ? backColorPressed : this.BackColor) : backColorDisabled);
			Pen buttonPen = new Pen((Enabled ? ( isPressed ? backColorPressed : this.BackColor) : foreColorDisabled), 1) ;

			//tmp image
			Bitmap img1 = new Bitmap(2 * radius + 1, 2 * radius +1);
			using (Graphics g1 = Graphics.FromImage(img1))
			{
				g1.Clear(chroma);

				g1.DrawEllipse(buttonPen, 0, 0, 2 * radius, 2 * radius);
				g1.FillEllipse(buttonBrush, 0, 0, 2 * radius, 2 * radius);
				//corner top-left 
				Rectangle dr1 = new Rectangle(0, 0, radius, radius);
				graphs.DrawImage(img1, 0, 0, dr1, GraphicsUnit.Pixel);
				//graphs.DrawImage(img1, dr1, 0, 0, 10, 10, GraphicsUnit.Pixel, imageattr);
				//graphs.DrawImage(img1, dr1, 0, 0, 10, 10, GraphicsUnit.Pixel, imageattr);
				//corner top-right
				dr1 = new Rectangle(radius + 1, 0, radius, radius);
				graphs.DrawImage(img1, this.Width - radius, 0, dr1, GraphicsUnit.Pixel);
				//corner bottom-left
				dr1 = new Rectangle(0, radius + 1, radius, radius);
				graphs.DrawImage(img1, 0, this.Height - radius, dr1, GraphicsUnit.Pixel);
				//corner bottpm-left
				dr1 = new Rectangle(radius + 1, radius + 1, radius, radius);
				graphs.DrawImage(img1, this.Width - radius, this.Height - radius, dr1, GraphicsUnit.Pixel);
			}

			//top and bottom line + vertical filling
			graphs.DrawLine(buttonPen, radius, 0, this.Width - radius, 0);
			graphs.DrawLine(buttonPen, radius, this.Height - 1, this.Width - radius, this.Height - 1);
			graphs.FillPolygon(buttonBrush, new Point[]
			                                	{
													new Point( radius, 0),
													new Point(this.Width - radius, 0),
													new Point(this.Width - radius, this.Height-1),
													new Point(radius, this.Height-1)
		});

			//left and right line + horisontal filling
			graphs.DrawLine(buttonPen, 0, radius, 0, this.Height - radius);
			graphs.DrawLine(buttonPen, this.Width - 1, radius, this.Width - 1, this.Height - radius);
			graphs.FillPolygon(buttonBrush, new Point[]
			                                	{
													new Point( 0, radius),	
													new Point(0, this.Height - radius),
													new Point(this.Width-1, this.Height-radius),
													new Point(this.Width - 1, radius)
													
			                                		
		});

			//text positioning
			int startposW = 0;
			int startposH = 0;
			// we're smaller than width of image, so lets try and centre
			if (fontsize.Width < this.Width)
			{
				startposW = Convert.ToInt32((this.ClientSize.Width - fontsize.Width) / 2);
				startposH = Convert.ToInt32((this.ClientSize.Height - fontsize.Height) / 2);
			}

			SolidBrush captionBrush = new SolidBrush(Enabled ? (isPressed ? foreColorPressed : this.ForeColor) : foreColorDisabled);
			graphs.DrawString(caption, this.Font, captionBrush, startposW, startposH);


			// clean up resources
			captionBrush.Dispose();
			buttonBrush.Dispose();
			buttonPen.Dispose();
			graphs.Dispose();
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{
			e.Graphics.Clear(this.Parent.BackColor);
			//e.Graphics.Clear(chroma);
			//e.Graphics.Clear(Color.Transparent);
			//base.OnPaintBackground(e);
		}


		private void ucbuttonEx_MouseDown(object sender, MouseEventArgs e)
		{
			this.isPressed = true;
			bufferimage = null;
			this.Invalidate();
		}

		private void ucbuttonEx_LostFocus(object sender, EventArgs e)
		{
			isPressed = false;
			bufferimage = null;
			this.Invalidate();
		}

		private void ucbuttonEx_MouseUp(object sender, MouseEventArgs e)
		{
			isPressed = false;
			bufferimage = null;
			this.Invalidate();
		}

		public new void Dispose()
		{
			ourimage.Dispose();
			bufferimage.Dispose();
		}
	}
}
